package com.rms.hkpc.util;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import com.rms.hkpc.MyApplication;
import com.rms.hkpc.R;
import com.rms.hkpc.constant.AllConstants;

import java.util.Locale;

public class LanguageUtils {
    private static volatile LanguageUtils instance;

    private LanguageUtils() {
    }

    public static LanguageUtils getInstance() {
        if (null == instance) {
            synchronized (LanguageUtils.class) {
                if (instance == null) {
                    instance = new LanguageUtils();
                }
            }
        }
        return instance;
    }

    public Context change(Locale locale) {
        Resources resources = MyApplication.getContext().getApplicationContext().getResources();
        Configuration config = resources.getConfiguration();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            config.setLocale(locale);
        } else {
            config.locale = locale;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocales(new LocaleList(locale));
            return MyApplication.getContext().createConfigurationContext(config);
        } else {
            DisplayMetrics dm = resources.getDisplayMetrics();
            resources.updateConfiguration(config, dm);
            return MyApplication.getContext();
        }
    }


    /**
     * 获取当前语言
     *
     * @return
     */
    public Locale getCurrentLanguage() {
        int language = SPUtil.getInstance().getInt(AllConstants.LANGUAGE, 1);
        if (language == 1) {
            return Locale.CHINA;
        } else if (language == 2) {
            return Locale.ENGLISH;
        }
        return Locale.CHINA;
    }

    /**
     * 获取当前语言
     *
     * @return
     */
    public int getStrLanguage() {
        int language = SPUtil.getInstance().getInt(AllConstants.LANGUAGE, 1);
        if (language == 1) {
            return R.string.chinese;
        } else if (language == 2) {
            return R.string.english;
        }
        return R.string.chinese;
    }

    public String getLanguage() {
        int language = SPUtil.getInstance().getInt(AllConstants.LANGUAGE, 1);
        if (language == 1) {
            return "zh-TW,zh";
        } else if (language == 2) {
            return "en-US,en";
        }
        return "zh-TW,zh";
    }
}
